package com.example.Arithmetic.Lettcode;

import com.example.Arithmetic.Arithmetic.MinHeap;

/**
 * 日期：2023/10/13
 * 时间：14:08
 * 描述：求数组中第k大的元素
 */
public class E04Lettcode215 {
    public int findKthLargest(int[] nums, int k) {
        MinHeap minHeap = new MinHeap(k);
        for (int i = 0; i < k; i++) {
            minHeap.offer(nums[i]);
        }
        for (int i = k; i < nums.length; i++) {
            if (nums[i] > minHeap.peek()) {
                minHeap.replace(nums[i]);
            }
        }

        return minHeap.peek();
    }

    public static void main(String[] args) {
        E04Lettcode215 e04Lettcode215 = new E04Lettcode215();
        int[] array = new int[]{5, 2, 4, 1, 3, 6, 0};
        int kthLargest = e04Lettcode215.findKthLargest(array, 4);
        System.out.println(kthLargest);
    }
}